import { createRouter, createWebHistory } from 'vue-router';
import power from './assets/utils/power';

// 路由信息
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('./views/login/index.vue'),
    children: [],
  },
  {
    path: '/layoutForMain',
    name: 'layoutForMain',
    component: () => import('./views/layoutForMain/index.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('./views/layoutForMain/home/index.vue'),
      },
      {
        path: '/customerList',
        name: 'customerList',
        component: () => import('./views/layoutForMain/customerList/index.vue'),
      },
      {
        path: '/permissionsetting',
        name: 'permissionsetting',
        component: () => import('./views/layoutForMain/permissionsetting/index.vue'),
      },
      {
        path: '/404',
        name: '404',
        component: () => import('./views/layoutForMain/404/index.vue'),
      },
    ],
  },
];

// 导出路由
const router = createRouter({
  history: createWebHistory(),
  routes,
});
let specialName = ['layoutForMain', 'login', 'home', '404']; // 无需校验权限的路由name

router.beforeEach((to, form, next) => {
  // 如果跳转页面不带name，默认不是本系统内的页面全部拦截到404页面
  if (!to.name) next('/404');
  // 权限列表
  let powerList = sessionStorage['powerList'] && JSON.parse(sessionStorage['powerList']);
  // 权限判断状态, 如果是true则代表有权限
  let powerStatus = power(powerList, to, specialName);
  if (powerStatus) {
    next();
  } else {
    // 无权限跳转
    ELEUI.Message.warning('该页面不存在或者您暂无权限访问该页面，请联系管理员后刷新重试');
    next('/home');
  }
});

export default router;
